package lesson01;

import com.sun.javaws.IconUtil;

/**
 * 多线程的优势-增加运行速度
 *
 * @author wileda
 * @date 2023/01/03  17:51
 */
public class Demo07 {
    static long count = 10_0000l;
    public static void main(String[] args) {
        serial();
        concurrency();
    }

    private static void concurrency() {
        long begin = System.currentTimeMillis();
        Thread t1 = new Thread(() -> {
            long a = 0l;
            for (int i = 0; i < count; i++) {
                a++;
            }
        });

        Thread t2 = new Thread(() ->{
            long b = 0l;
            for (int i = 0; i < count; i++) {
                b++;
            }
        });

        t1.start();
        t2.start();

        try {
            //等待
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        long end = System.currentTimeMillis();
        System.out.println("并行操作耗时：" + (end - begin) + "ms.");
    }

    private static void serial() {
        long begin = System.currentTimeMillis();
        long a = 0l;
        for (int i = 0; i < count; i++) {
            a++;
        }
        long b = 0l;
        for (int i = 0; i < count; i++) {
            b++;
        }
        long end = System.currentTimeMillis();
        System.out.println("串行操作耗时：" + (end - begin) + "ms.");
    }
}
